﻿using System.Collections.Generic;
using GalaSoft.MvvmLight;

namespace Gumini.JulaDB.Navigator.Logic
{
    public class ConnectionProvider : ObservableObject
    {
        JulaDbEngine _db;

        public bool IsConnected
        {
            get { return _isConnected; }
            private set
            {
                if (value != _isConnected)
                {
                    _isConnected = value;
                    RaisePropertyChanged("IsConnected");
                }
            }
        }
        bool _isConnected = false;

        public string DataSource
        {
            get { return _dataSource; }
            private set
            {
                if (value != _dataSource)
                {
                    _dataSource = value;
                    RaisePropertyChanged("DataSource");
                }
            }
        }
        string _dataSource;

        public void ConnectToInMemoryDatabase()
        {
            _db = JulaDbEngine.CreateInMemory();
            this.IsConnected = true;
            this.DataSource = "memory";
        }

        public void ConnectToFileDatabase(string filePath)
        {
            _db = JulaDbEngine.OpenOrCreateFile(filePath);
            this.IsConnected = true;
            this.DataSource = filePath;
        }

        public void Disconnect()
        {
            _db.Close();
            _db = null;
            this.IsConnected = false;
            this.DataSource = null;
        }

        public IList<CommandResult> Execute(string userInput, bool useOptimizer)
        {
            return _db.Execute(userInput, useOptimizer: useOptimizer);
        }
    }
}
